package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.Card;
import com.woniu.service.ICardService;
import com.woniu.service.IMemberService;
import com.woniu.vo.BuyCardVo;
import com.woniu.vo.QueryCardVo;
import com.woniu.vo.cardVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniu.vo.CardSearchVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zbk
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/card")
@Api(tags = "会员卡管理")
public class CardController extends BaseController{

    @Autowired
    private ICardService service;
    @Autowired
    private IMemberService memberService;

    @ApiOperation("根据团课id查询会员卡")
    @GetMapping("/league/{leagueId}")
    public Result queryByLeagueId(@PathVariable String leagueId){
        List<CardSearchVo> cardSearchVoList = service.queryByLeagueId(leagueId);
        return toDataResult(cardSearchVoList);
    }

    @ApiOperation("查询全部")
    @GetMapping
    public Result queryAll(){
        List<Card> cardList = service.list();
        return toDataResult(cardList);
    }

    @ApiOperation("条件分页查询")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result queryByItemInPage(@PathVariable long currentPage, @PathVariable long pageSize,
                                    @RequestBody Card card){
        PageResult result=service.queryByItemInPage(currentPage,pageSize,card);
        return toDataResult(result);
    }

    @ApiOperation("通过id查找")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable String id){
        Card card = service.getById(id);
        return toDataResult(card);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result modify(@RequestBody Card card){
        boolean flag = service.updateById(card);
        return toResult(flag);
    }

    @ApiOperation("新增")
    @PostMapping
    public Result add(@RequestBody Card card){
        boolean flag = service.save(card);
        return toResult(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        boolean flag = service.removeById(id);
        return toResult(flag);
    }
    @ApiOperation("查询储蓄类型卡")
    @GetMapping("/byCardType")
    public Result byCardType(){
        List<cardVo> cardVo = service.byCardType();
        return toDataResult(cardVo);
    }

    @ApiOperation("根据会员id查询会员卡")
    @GetMapping("/member")
    public Result queryByMemberId(HttpSession session){
        String tel = (String) session.getAttribute("tel");
        String memberId = memberService.queryMemberId(tel);
        List<Card> cardList = service.queryByMemberId(memberId);
        return toDataResult(cardList);
    }

    @ApiOperation("通过私课课表id查询会员卡")
    @GetMapping("/app/{id}")
    public Result byTimetableId(@PathVariable String id){
        List<Card> cardList = service.byTimetableId(id);
        return toDataResult(cardList);
    }

    @ApiOperation("查询所有可购买的卡")
    @GetMapping("/buy")
    public Result queryAllBuy(){
        List<BuyCardVo> vo=service.queryAllBuy();
        return toDataResult(vo);
    }

    @ApiOperation("模拟点击会员卡")
    @GetMapping("/buy/{id}")
    public Result queryBuyById(@PathVariable String id){
        BuyCardVo vo=service.queryBuyById(id);
        return toDataResult(vo);
    }

    @ApiOperation("购买会员卡")
    @PostMapping("/buy")
    public Result buyCard(@RequestBody BuyCardVo vo){
        int row=service.buyCard(vo);
        return toResult(row);
    }

    @ApiOperation("通过会员id查找会员卡信息")
    @GetMapping("/member/{memberId}")
    public Result queryByMemberId(@PathVariable String memberId){
        List<Card> cardList=service.queryByMemberId(memberId);
        return toDataResult(cardList);
    }
}
